<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\service\GatewayClientServer;
use app\index\dao\ConsultingDao;
use app\index\model\UM as UM;
use ShidianUtil\Exception\DataNotExistException;
use ShidianUtil\Utils\TimeUtil;

/**
 * Class ConsultingHiddenService
 * @package app\index\service
 */
class ConsultingHiddenService
{
    // dao
    private ConsultingDao $consultingDao;

    public function __construct(ConsultingDao $consultingDao)
    {
        $this->consultingDao = $consultingDao;
    }

    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     */
    public function modify(UM $user, int $id, array $params): void
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = Consulting::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该咨询不存在或已被删除');
        }

        if ($record->memberId == $user->id) {
            $record->memberHiddenTime = TimeUtil::millitime();
            $record->save();
        } elseif ($record->lawyerId == $user->id) {
            $record->lawyerHiddenTime = TimeUtil::millitime();
            $record->save();
        }

        // 发红点更新推送
        $data = [
            'action' => REDDOT,
            'data' => [],
        ];
        $this->getGatewayClientServer()->sendToUid($user->id, json_encode($data));
    }

    private function getGatewayClientServer(): GatewayClientServer
    {
        return app(GatewayClientServer::class);
    }
}
